package com.hwg.emp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hwg.emp.common.util.DBUtil;
import com.hwg.emp.dao.ModuleDAO;
import com.hwg.emp.model.ModuleMD;
import com.hwg.emp.model.RoleMD;

public class ModuleDAOImpl implements ModuleDAO {

	@Override
	public int insert(ModuleMD t) throws Exception {
		return 0;
		
	}

	@Override
	public int delete(String uk, String ukValue) throws Exception {
		return 0;
		
	}

	@Override
	public int update(ModuleMD t) throws Exception {
		return 0;
		
	}

	@Override
	public ModuleMD select(String uk, String ukValue) throws Exception {
		return null;
	}

	@Override
	public boolean exist(String uk, String ukValue) throws Exception {
		return false;
	}

	@Override
	public List<ModuleMD> selectParentModuleByRoleID(String id) throws Exception {
		List<ModuleMD> modules = new ArrayList<>();
		Connection con=DBUtil.getConnection();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from rel_role_module rm ")
		.append("join sys_module m on rm.module_id=m.id ")
		.append("where rm.role_id=?");
		PreparedStatement ps=con.prepareStatement(sql.toString());
		ps.setObject(1, id);
		ResultSet rs=ps.executeQuery();
		while(rs.next()){
			modules.add(result2md(rs));
		}
		return modules;
	}

	/**
	 * 拼凑结果集
	 * @param rs 传入一个结果集
	 * @return 用户对象
	 * @throws SQLException
	 */
	private ModuleMD result2md(ResultSet rs)throws SQLException{
		ModuleMD md=new ModuleMD();
		md.setId(rs.getString("id"));
		md.setName(rs.getString("name"));
		md.setRemark(rs.getString("remark"));
		md.setParentId(rs.getString("parent_id"));
		md.setStatus(rs.getInt("status"));
		md.setUrl(rs.getString("url"));
		return md;
	}
	
	@Override
	public List<ModuleMD> selectModuleByParentId(String id) throws Exception {
		List<ModuleMD> modules = new ArrayList<>();
		Connection con=DBUtil.getConnection();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from sys_module where parent_id=?");
		PreparedStatement ps=con.prepareStatement(sql.toString());
		ps.setObject(1, id);
		ResultSet rs=ps.executeQuery();
		while(rs.next()){
			modules.add(result2md(rs));
		}
		return modules;
	}

}
